

export interface CodecBase {

    encode(buf: number[], index: number): void,
    decode(buf: number[], index: number): void,
    size(): number,
}

class Uint8 implements CodecBase {

    value: number;

    constructor(value: number = 0) {
        this.value = value;
    }

    encode(buf: number[], index: number): void {
        buf[index] = this.value & 0xFF;
    }
    decode(buf: number[], index: number): void {
        this.value = buf[index];
    }
    size(): number {
        return 1;
    }

    isSet(bit: number) {
        return (this.value & (0x01 << bit)) != 0;
    }
}

export class Uint16 implements CodecBase {

    value: number;

    constructor(value: number = 0) {
        this.value = value;
    }

    encode(buf: number[], index: number): void {
        buf[index] = (this.value >> 8) & 0xFF;
        buf[index + 1] = this.value & 0xFF;
    }
    decode(buf: number[], index: number): void {
        this.value = (buf[index] << 8) + buf[index + 1];
    }
    size(): number {
        return 2;
    }

    isSet(bit: number) {
        return (this.value & (0x01 << bit)) != 0;
    }
}

export class Uint32 implements CodecBase {

    value: number;

    constructor(value: number = 0) {
        this.value = value;
    }

    encode(buf: number[], index: number): void {
        buf[index] = (this.value >> 24) & 0xFF;
        buf[index + 1] = (this.value >> 16) & 0xFF;
        buf[index + 2] = (this.value >> 8) & 0xFF;
        buf[index + 3] = this.value & 0xFF;
    }
    decode(buf: number[], index: number): void {
        this.value = (buf[index] << 24) +
            (buf[index + 1] << 16) +
            (buf[index + 2] << 8) +
            (buf[index + 3]);
    }
    size(): number {
        return 4;
    }

    isSet(bit: number) {
        return (this.value & (0x01 << bit)) != 0;
    }
}

export function u8(value = 0) {
    return new Uint8(value);
}

export function u16(value = 0) {
    return new Uint16(value);
}

export function u32(value = 0) {
    return new Uint32(value);
}








